<template>
  <view>
    <nav-bar
      title="余额详情"
      bgColor="#ffb72f"
      titleColor="#fff"
      class="nav-bar"
      supportWhiteBlack
    />
    <view class="con-top">
      <text class="price" :style="{ color: themeColor }" v-if="detail"
        >{{ detail.balanceFee < 0 ? "-" : "" }}￥{{
          detail.balanceFee | formatMoneyCurrency
        }}</text
      >
    </view>
    <view class="con-btm">
      <view class="li">
        <view class="left">门店</view>
        <view class="right">{{ detail.shopName || "-" }}</view>
      </view>
      <view class="li">
        <view class="left">类型</view>
        <view class="right"> {{ BALANCE_RECORD_TYPE[detail.recordType] }}</view>
      </view>
      <view class="li">
        <view class="left">变动时间</view>
        <view class="right">{{ detail.createTime }}</view>
      </view>
      <view class="li" v-if="detail.recordType === BALANCE_RECORD_TYPE">
        <view class="left">充值单号</view>
        <view class="right">{{ detail.bizOrderNo }}</view>
      </view>
      <view class="li">
        <view class="left">剩余余额</view>
        <view class="right" v-if="detail"
          >￥{{ detail.balance | formatMoneyCurrency }}</view
        >
      </view>
      <view class="li">
        <view class="left">备注</view>
        <view class="right">{{ detail.remark || "无" }}</view>
      </view>
    </view>
  </view>
</template>

<script>
import { BALANCE_RECORD_TYPE } from "@/pages/sp-member/const/member";

export default {
  data() {
    return { BALANCE_RECORD_TYPE, navBarHeight: 60, detail: null };
  },
  filters: {
    // 金钱格式化
    formatMoneyCurrency(num) {
      if (!num) return "0.00";
      num = Number(num) / 100;
      num = num.toString().replace(/\$|,/g, "");
      if (isNaN(num)) num = "0";
      let sign = Number(num) === (num = Math.abs(num));
      num = Math.floor(num * 100 + 0.50000000001);
      let cents = num % 100;
      num = Math.floor(num / 100).toString();
      if (cents < 10) cents = "0" + cents;
      for (let i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num =
          num.substring(0, num.length - (4 * i + 3)) +
          "," +
          num.substring(num.length - (4 * i + 3));
      return num + "." + cents;
    },
  },
  computed: {
    themeColor({ $theme }) {
      return $theme.themeColor.primary;
    },
  },
  mounted() {
    const navBar = this.uGetRect(".nav-bar");
    Promise.all([navBar]).then((res) => {
      this.navBarHeight = res[0].height;
    });
    this.getEventChannel();
  },
  methods: {
    getEventChannel() {
      const eventChannel = this.getOpenerEventChannel();
      if (JSON.stringify(eventChannel) !== "{}") {
        eventChannel.on("fetchData", (res) => {
          this.detail = res;
          console.log("页面通信", res);
        });
      }
    },
    uGetRect(selector, all) {
      return new Promise((resolve) => {
        uni
          .createSelectorQuery()
          .in(this)
          [all ? "selectAll" : "select"](selector)
          .boundingClientRect((rect) => {
            if (all && Array.isArray(rect) && rect.length) {
              resolve(rect);
            }
            if (!all && rect) {
              resolve(rect);
            }
          })
          .exec();
      });
    },
    fetchData() {},
  },
};
</script>

<style lang="scss" scoped>
.con-top {
  border-bottom: 1rpx solid whitesmoke;
  text-align: center;
  padding: 50rpx 0;
  box-sizing: border-box;
  font-weight: bold;
  font-size: 46rpx;
  .price {
    font-weight: normal;
    font-size: 48rpx;
    margin-left: 20rpx;
  }
}
.con-btm {
  padding: 50rpx 22rpx;
  box-sizing: border-box;
  font-size: 29rpx;
  .li {
    display: flex;
    //justify-content: space-between;
    margin-bottom: 28rpx;
    .left {
      width: 200rpx;
      color: #888888;
    }
    .right {
      font-weight: 500;
    }
  }
}
</style>
